package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-10-18
 * Time: 16:44
 */
//针对博客要实现的内容
//1.新增博客(博客编辑页)
//2.查询出博客列表(博客列表页)
//3.查询出指定博客的详情(博客详情页)
//4.删除指定博客
public class BlogDao {
    //这里的Blog对象是前端提交给后端的
    public void insert(Blog blog){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造sql语句
            String sql = "insert into blog values(null, ?, ?, ?, now())";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            //执行sql
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("新增成功!");
            }else{
                System.out.println("新增失败!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, null);
        }
    }

    //显示博客列表页中每条博客的简介内容
    public List<Blog> selectAll(){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> blogs = new ArrayList<Blog>();
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造sql语句
            String sql = "select * from blog";
            statement = connection.prepareStatement(sql);
            //执行sql
            resultSet = statement.executeQuery();
            //遍历结果集合
            while(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                //由于是列表, 只需截取前80个字作为摘要即可
                blog.setContent(content.substring(0, 80));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return blogs;
    }

    public Blog selectOne(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造sql语句
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            //遍历结果集, 由于是按照blogId来进行查询, 所以查询到的结果只能是0或者1
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public void delete(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造sql语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            //执行sql
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("删除成功!");
            }else{
                System.out.println("删除失败!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, null);
        }
    }
}
